<?php
namespace app\store\model;
use app\common\model\TestUser as TestUserModel;

/**
 * 小程序体验者用户模型
 */
class TestUser extends TestUserModel
{
    /**
     * 添加
     */
    public function add($data)
    {
		$access_token = getAccessToken();		
		$url = 'https://api.weixin.qq.com/wxa/bind_tester?access_token='.$access_token;
		$post = '{"wechatid":"'.$data['wechatid'].'"}';
		$result = json_decode(http_request($url,$post),true);
		if($result['errcode']!= 0){
			switch($result['errcode']){
				case 85001:
					$this->error = '微信号不存在或微信号设置为不可搜索';
					break;
				case 85002:
					$this->error = '小程序绑定的体验者数量达到上限';
					break;
				case 85003:
					$this->error = '微信号绑定的小程序体验者达到上限';
					break;
				case 85004:
					$this->error = '微信号已经绑定';
					break;
				default:
					$this->error = '错误代码：'.$result['errcode'].'，'.$result['errmsg'];
			}
            return false;
		}
		$data['userstr'] = $result['userstr'];
        $data['wxapp_id'] = self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }

    /**
     * 删除
     */
    public function remove()
    {
		$access_token = getAccessToken();		
		$url = 'https://api.weixin.qq.com/wxa/unbind_tester?access_token='.$access_token;
		$post = '{"userstr":"'.$this->userstr.'"}';
		$result = json_decode(http_request($url,$post),true);
		if($result['errcode']!= 0){
			$this->error = '错误代码：'.$result['errcode'].'，'.$result['errmsg'];
            return false;
		}
        return $this->delete();
    }
}
